/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "StdAfx.h"
#include "TrueTypeFont.h"

namespace directx {

TrueTypeFont::TrueTypeFont(void)
:	pFont(NULL)
{
	//setColor(vColor);
	//calcCharSize();
}

TrueTypeFont::~TrueTypeFont(void)
{
	release();
}

void TrueTypeFont::release()
{
	// Free the font pointer
	RELEASE( pFont );
}

void TrueTypeFont::reset()
{
	// Get the d3dDevice -> see DXResource
	DXResource::reset();

	if (bLoaded) {
		load(sFilename, iSize);
	}
}

bool TrueTypeFont::load(const std::string &fontname, int size, int style)
{
	UINT weight = FW_NORMAL;
	if (style & FONT_BOLD) 
		weight = FW_BOLD;
	
	sFilename = fontname;
	iSize = size;
	if (FAILED( D3DXCreateFont( pd3dDevice, 
								size, 
								0, 
								weight, 
								1, 
								(style & FONT_ITALIC), 
								DEFAULT_CHARSET, 
								OUT_TT_ONLY_PRECIS, 
								ANTIALIASED_QUALITY, 
								DEFAULT_PITCH | FF_DONTCARE, 
								fontname.c_str(), 
								&pFont) ))
	{
		coreGetLog()->WriteError("Could not create font " + fontname, __FILE__, __LINE__);
		return false;
	}

	bLoaded = true;
	coreGetLog()->WriteLog("Font " + fontname + " created successfully.", __FILE__, __LINE__);
	return true;
}

void TrueTypeFont::print(const std::string &str, int x, int y, int z, int align, int width, int height, const math::Vector4 &color)
{
	if (!bLoaded) {
		coreGetLog()->WriteWarning("Cannot print line, no font loaded", __FILE__, __LINE__);
		return;
	}

	DWORD format = 0;

	// Define a rectangle for the font
	if (width == 0 && height == 0) {
		SetRect(&font_rect, x, y, iSize, iSize);
		format |= DT_NOCLIP; 
	}
	else {
		SetRect(&font_rect, x, y, x+width, y+height);
		format = DT_WORDBREAK;
	}

	if (align == FONT_LEFT) 
		format |= DT_LEFT;
	else if (align == FONT_RIGHT)
		format |= DT_RIGHT;
	else if (align == FONT_CENTER) 
		format |= DT_CENTER;

	if (pFont) {
		pFont->DrawText(NULL,			// pSprite
						str.c_str(),		// pString
						-1,					// Count
						&font_rect,			// pRect
						format,				// Format,|DT_NOCLIP
						D3DCOLOR_COLORVALUE(color.x, color.y, color.z, color.w));			// Color
	} 
	else {
		coreGetLog()->WriteWarning("No font loaded", __FILE__, __LINE__);
	}
}

void TrueTypeFont::calcCharSize()
{
	char c;
	for (int i=0; i<NUMCHARS; ++i) {
		c = i+32;
		std::string str(&c);
		
		pFont->DrawText(NULL,			// pSprite
			str.c_str(),		// pString
						-1,					// Count
						&font_rect,			// pRect
						DT_NOCLIP | DT_LEFT,				// Format,|DT_NOCLIP
						D3DCOLOR_COLORVALUE(1,1,1,1));// Color
		STRINGCOUT(font_rect.right);		
	}
}

int TrueTypeFont::getLength(const std::string &str)
{
	pFont->DrawText(NULL, str.c_str(),-1, &font_rect, DT_CALCRECT, D3DCOLOR_COLORVALUE(1,1,1,1));
	return (int) font_rect.right - font_rect.left;
	//return (int) str.length() * iSize;
}

} // namespace
